/*
  * @file sorter.cpp
  * @author Thibault Merle et Guillaume Poussel
  */
#include "base/sorter.h"
#include "base/geometry.h"
#include "base/Vector.h"

bool Point_X_Order::operator()(const Point* p1, const Point* p2) const {
    return p1->x() < p2->x();
}

bool Point_Y_Order::operator()(const Point* p1, const Point* p2) const {
    return p1->y() < p2->y();
}

bool Lexicographic_Order::operator()(const Point* p1, const Point* p2) const {
    return (*p1) < (*p2);
}


bool Clockwise_Order::operator()(const Point* pA, const Point* pB) const {
    if (*pA == *_ref) {
        return true;
    } else if (*pB == *_ref) {
        return false;
    }
    Vector vA(_O, pA);
    Vector vB(_O, pB);
    Vector vRef(_O, _ref);
    bool acw_A_B = (cross_product(&vA, &vB) > 0);
    bool acw_ref_A = (cross_product(&vRef, &vA) > 0);
    bool acw_ref_B = (cross_product(&vRef, &vB) > 0);
    if (acw_ref_A == acw_ref_B) {
        return acw_A_B != _clock_wise;
    } else if (acw_ref_A) {
        return !_clock_wise;
    } else {
        return _clock_wise;
    }
}

bool Point_Comparator::operator()(const Point* p1, const Point* p2) const {
    return (*p1) == (*p2);
}
